<template>
  <div class="container">
    <div class="title">
      <span>添加题目</span> <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
    </div>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
            <el-form-item label="试卷名称" prop="aswtype">
              <el-input size="medium" v-model="form.aswtype" placeholder="请填写试卷名称"></el-input>
            </el-form-item>
            <el-form-item label="试题标题" prop="aswtitle">
              <el-input size="medium" v-model="form.aswtitle" placeholder="请填写试题标题"></el-input>
            </el-form-item>
            <el-form-item label="题目分类" prop="sortid">
              <el-select v-model="form.sortid" placeholder="请选择">
                <el-option v-for="item in sorts" :key="item.id" :label="item.asw_type_name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="题目类型" prop="chcktype">
              <el-select v-model="form.chcktype" placeholder="请选择">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"
                  @click.native="selectCertType2(item)">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="选项" v-if="form.chcktype ===1 || form.chcktype ===2">
              <el-form :inline="true" label-width="100px">
                <div v-for="(item, index) in form.dynamicitem" :key="index">
                  <el-form-item label="选项" :prop="'dynamicitem.' + index + '.name'">
                    <el-input v-model="item.name" placeholder="eg:A"></el-input>
                  </el-form-item>
                  <el-form-item label="内容" :prop="'dynamicitem.' + index + '.text'">
                    <el-input v-model="item.text"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button @click="deleteItem(item, index)" type="danger" icon="el-icon-delete" circle></el-button>
                    <!-- <el-button v-if="index !== 0" @click="deleteItem(item, index)" type="danger">删除</el-button>-->
                  </el-form-item>
                </div>
                <el-form-item>
                  <el-button @click="addItem" type="primary">添加选项</el-button>
                </el-form-item>
              </el-form>
            </el-form-item>
            <el-form-item label="正确答案" prop="trueasw">
              <el-input size="medium" type="textarea" :autosize="{ minRows: 3, maxRows: 8 }" v-model="form.trueasw"
                placeholder="请填写正确答案"></el-input>
            </el-form-item>
            <el-form-item label="答案解析" prop="analysistxt">
              <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 8 }" size="medium" v-model="form.analysistxt"
                placeholder="请填写答案解析"></el-input>
            </el-form-item>

            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm('form')" :loading="loading">发 布</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import asw from '@/model/problem.js'

export default {
  data() {
    return {
      form: {
        aswtitle: '',
        aswtype: '',
        chcktype: '',
        trueasw: '',
        analysistxt: '',
        sortid: '',
        dynamicitem: [{
          name: '',
          text: ''
        }]
      },
      sorts: [],
      options: [{
        value: 1,
        label: '单选题'
      }, {
        value: 2,
        label: '多选题'
      }, {
        value: 3,
        label: '简答题'
      }],
      loading: false,
    }
  },
  async created() {
    this.loading = true
    const aswTypes = await asw.getAswTypes()
    this.sorts = aswTypes
    this.loading = false
  },
  methods: {
    async submitForm(formName) {
      // var a = JSON.stringify(this.form.dynamicitem)
      // var b = JSON.stringify(this.form)
      try {
        this.loading = true
        const res = await asw.createAsw(this.form)
        this.loading = false
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.$message.success(`${res.message}`)
          this.resetForm(formName)
          this.form.dynamicitem = [{
            name: '',
            text: ''
          }]
        }
      } catch (error) {
        this.loading = false
        this.$message.error('试题添加失败，请检测填写信息')
        console.log(error)
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('addClose')
    },
    addItem() {
      this.form.dynamicitem.push({
        name: '',
        text: ''
      })
    },
    deleteItem(item, index) {
      this.form.dynamicitem.splice(index, 1)
    },
    selectCertType2(data) {
      this.form.chcktype = data.value
    },
  },
}
</script>

<style lang="scss" scoped>
  .container {
    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
      text-indent: 40px;
      border-bottom: 1px solid #dae1ec;
    }

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }

    .wrap {
      padding: 20px;
    }

    .submit {
      float: left;
    }
  }
</style>
